<script>
	import Vue from 'vue'
	export default {
		onLaunch: function(option) {
			// console.log('App Launch')
			// uni.setLocale('zh-Hans')
			// uni.setLocale('en')
			// console.log('option',option)
			
			
			this.globalData.user_token = this.$ZC('user_token')
			this.globalData.user_info = this.$ZC('user_info')
			// 静默登入
			this.autoLogin()
			
			if(this.$Route.query.lang == 'zh' || this.$Route.query.lang == 'zh-Hans'){
				uni.setLocale('zh-Hans');
				this.$i18n.locale = 'zh-Hans';
			}
			else if(this.$Route.query.lang == 'en'){
				uni.setLocale('en');
				this.$i18n.locale = 'en';
			}
			
						
			Vue.prototype.$getLocaleField = (item, field)=>{
				var res = item[field]
				if(this.$i18n.locale == 'zh-Hans' && item[field+'_cn']){
					res = item[field+'_cn']
				}
				return res
			}
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		globalData: {
			user_info: false,
			user_token: false,
			api_lang: 'zh-Hans',
		},
		methods:{
			autoLogin(){
				// console.log(this.globalData.user_token)
				if(this.globalData.user_token){
		            console.log('autoLogin...');
					this.$httpGet({
						url: "api/getUserInfo",
						passLoading: 1,
						fail: (res)=>{
							if(res.code == 406){
								this.$msg(res.message,'none',()=>{
									this.$ZC.clear()
									this.globalData.user_info = null
									this.globalData.user_token = null
									this.$navTo('/')
								})
							}
						}
					}).then((res)=>{
						// console.log(11111,res)
						this.$ZC('user_info', res.data, -1)
						// ZC('user_token', res.data.token, 86400)
						this.globalData.user_info = res.data
						// this.globalData.user_token = res.data.token
						uni.$emit("userLogin", this.globalData.user_info);
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/style/index.scss";
	@media (min-width: 800px) {
		.uni-page-head,.uni-body{
			max-width: 500px;
			margin: auto;
		}
	}
</style>
